import 'dart:io';

import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:aio/aio.dart' as aio;
import 'package:app/generated/app_strings.dart';

import '../../http/bean/worksdo/equipment_management_bean.dart';
import '../../http/bean/worksdo/material_requisition_bean.dart';
import '../../http/bean/ywx_limit.dart';
import '../../manager/ywx_manager.dart';
import '../route/helper/route_helper.dart';

class AppDialog {
  ///删除弹窗
  static showDialogWithDel(@required BuildContext? context,
      {String? title = null,
      String? content = '',
      bool isOk = true,
      bool isCenterContent = true,
      ResponseCallbackWithBool? response = null}) {
    if (context == null) {
      return;
    }
    if (StringUtils.isEmpty(content)) {
      content = AppStrings.of(context).confirmDelete; //确认删除
    }
    aio
        .showDialogWithAlert(context,
            title: title,
            content: content,
            isOk: isOk,
            isCenterContent: isCenterContent)
        ?.then((value) {
      if (value != null) {
        response?.call(value);
      }
    });
  }

  ///统一判断弹窗是否有显示。
  static bool isShowDialog() {
    if (isShowDialogWithAlert()) {
      return true;
    }
    if (isShowDialogWithInfo()) {
      return true;
    }
    if (aio.isShowDialog()) {
      return true;
    }
    return false;
  }

  static bool isShowDialogWithAlert() {
    return aio.isShowDialogWithAlert();
  }

  ///Alert确认弹框(有取消和确定两个按钮)
  static Future<bool?>? showDialogWithAlert(@required BuildContext? context,
      {String? title = null,
      String? content = '',
      String? okText = null,
      String? cancelText = null,
      bool isOk = true,
      bool isCenterContent = true}) {
    return aio.showDialogWithAlert(context,
        title: title,
        content: content,
        isOk: isOk,
        okText: okText,
        cancelText: cancelText,
        isCenterContent: isCenterContent);
  }

  static bool isShowDialogWithInfo() {
    return aio.isShowDialogWithInfo();
  }

  ///信息提示弹框（只有一个确认按钮）
  static Future<bool?> showDialogWithInfo<bool>(@required BuildContext? context,
      {String? content}) {
    return aio.showDialogWithInfo(context, content: content);
  }

  ///滚轮选择器
  ///datas 滚轮数据集合
  ///data 当前选中值
  static Future<T?> showDialogWithRollerPicker<T extends aio.RollerBean>(
    @required BuildContext? context, {
    List<T?>? datas,
    T? data,
    String? key,
    String? value,
  }) {
    return aio.showDialogWithRollerPicker<T>(context,
        datas: datas, data: data, key: key, value: value);
  }

  ///图片选择弹窗
  ///headPath 头像地址,如果不为空，可查看头像图片
  static showDialogWithPickerPhoto(
      BuildContext? context, ResponseCallbackWithT<File>? callback,
      {String? headPath, bool isFileSelect = false}) {
    aio.showDialogWithPickerPhoto(context, callback,
        headPath: headPath, isFileSelect: isFileSelect);
  }

  ///拍摄图片和视频
  static showDialogWithTakePicturesAndVideos(
      BuildContext? context, ResponseCallbackWithT<File>? callback,{bool isCompress = false,
        Duration? maxDuration = const Duration(seconds: 10)}) {
    aio.showDialogWithTakePicturesAndVideos(context, callback,isCompress: isCompress,maxDuration: maxDuration);
  }

  ///显示网络请求错误信息
  static Future<bool?> showDialogWithError(
      BuildContext? context, String? code, String? error) {
    if (context == null) {
      return Future.value(false);
    }
    var navigator = Navigator.maybeOf(context);
    if (navigator == null) {
      return Future.value(false);
    }
    if (code != null && code.trim() == "0") {
      return showDialogWithInfo(context,
          content: AppStrings.of(context).net_error_0);
    } else if (code != null && code.trim() == "500") {
      return showDialogWithInfo(context,
          content: AppStrings.of(context).net_error_500);
    } else {
      return showDialogWithInfo(context,
          content: "Error:" + code.toString() + "\n" + error.toString());
    }
  }

  ///下载
  static showDialogWithDown(
    BuildContext context,
    String url, {
    bool againDownload = false,
    void response(File file)? = null,
  }) {
    aio.showDialogWithDown(context, url,
        againDownload: againDownload, response: response);
  }

  ///选择时间范围
  static Future<DateTimeRange?> showDialogWithDateRangePicker({
    @required BuildContext? context,
    DateTime? start, //开始时间
    DateTime? end, //结束时间
    DateTime? firstDate, //最小时间
    DateTime? lastDate, //最大时间
    bool dateAndTime = false, //dateAndTime true 年月日时分；false 年月日
    bool selectStart = true, //selectStart  true 选择开始日期；false 选中结束日期。
  }) {
    return aio.showDialogWithDateRangePicker(
        context: context,
        start: start,
        end: end,
        firstDate: firstDate,
        lastDate: lastDate,
        dateAndTime: dateAndTime,
        selectStart: selectStart);
  }

  ///日期选择
  static Future<DateTime?> showDialogWithDatePicker(
      {@required BuildContext? context,
      DateTime? initialDate,
      DateTime? firstDate,
      DateTime? lastDate,
      bool dateAndTime = false}) {
    return aio.showDialogWithDatePicker(
        context: context,
        initialDate: initialDate,
        firstDate: firstDate,
        lastDate: lastDate,
        dateAndTime: dateAndTime);
  }

  ///物料请购选择弹窗
  static showDialogWithPickerMaterialRequisition(BuildContext? context,
      YwxItem ywxItem, ResponseCallbackWithVoid callback) {
    BuildContext? _context = context;
    if (context == null) {
      return;
    }
    px.init(context);
    showCupertinoModalPopup(
        context: context!,
        barrierDismissible: true,
        builder: (context) {
          List<Widget> children = <Widget>[];
          //原片请购
          children.add(LibUi.addCenterItem(
              AppStrings.of(context).OriginalRequisition, () {
            LibRouteHelper.pop(context);
            ywxItem.ReviewType2 =
                ReviewType.MaterialRequisition_OriginalRequisition;
            ywxItem.YwxName2 = AppStrings.of(context).OriginalRequisition;
            callback?.call();
          }));
          //横线
          children.add(Divider(
            height: 1,
            color: Colors.black12,
          ));
          //辅料请购
          children.add(LibUi.addCenterItem(
              AppStrings.of(context).AccessoriesRequisition, () {
            LibRouteHelper.pop(context);
            ywxItem.ReviewType2 =
                ReviewType.MaterialRequisition_AccessoriesRequisition;
            ywxItem.YwxName2 = AppStrings.of(context).AccessoriesRequisition;
            callback?.call();
          }));
          //横线
          children.add(Divider(
            height: 1,
            color: Colors.black12,
          ));
          //五金请购
          children.add(LibUi.addCenterItem(
              AppStrings.of(context).HardwareRequisition, () {
            LibRouteHelper.pop(context);
            ywxItem.ReviewType2 =
                ReviewType.MaterialRequisition_HardwareRequisition;
            ywxItem.YwxName2 = AppStrings.of(context).HardwareRequisition;
            callback?.call();
          }));
          //横线
          children.add(Divider(
            height: 1,
            color: Colors.black12,
          ));
          //已生成
          children.add(LibUi.addCenterItem(
              AppStrings.of(context).RequisitionStatus1, () {
            LibRouteHelper.pop(context);
            ywxItem.ReviewType2 = ReviewType.MaterialRequisition_Generated;
            ywxItem.YwxName2 = AppStrings.of(context).RequisitionStatus1;
            callback?.call();
          }));
          //占位
          children.add(Expanded(
              child: Container(
            color: Colors.black12,
          )));
          //取消
          children.add(LibUi.addCenterItem(LibStrings.of(context).cancel, () {
            //取消
            LibRouteHelper.pop(context);
          }));
          var column = Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: children,
          );
          return Material(
            child: SizedBox(
              child: column,
              height: 258,
              width: px.screenWidth(),
            ),
          );
        });
  }

  ///我的工单->工单领用->选择弹窗
  static showDialogWithReceiptofworkorder(
      BuildContext? context, EquipmentInfo? data,YwxItem item) {
    BuildContext? _context = context;
    if (context == null) {
      return;
    }
    px.init(context);
    showCupertinoModalPopup(
        context: context!,
        barrierDismissible: true,
        builder: (context) {
          List<Widget> children = <Widget>[];
          //五金领用
          children.add(
              LibUi.addCenterItem(AppStrings.of(context).Hardwarecollar, () {
            LibRouteHelper.pop(context);
            AppRouteHelper.goReceiptofworkorderRoute(context!, data!);
          }));
          //横线
          children.add(Divider(
            height: 1,
            color: Colors.black12,
          ));
          //领用记录
          children.add(
              LibUi.addCenterItem(AppStrings.of(context).Receiptrecord, () {
            LibRouteHelper.pop(context);
            AppRouteHelper.goTreasuryListRoute(context!, item!,data!);
          }));
          // //横线
          // children.add(Divider(
          //   height: 1,
          //   color: Colors.black12,
          // ));
          //占位
          children.add(Expanded(
              child: Container(
            color: Colors.black12,
          )));
          //取消
          children.add(LibUi.addCenterItem(LibStrings.of(context).cancel, () {
            //取消
            LibRouteHelper.pop(context);
          }));
          var column = Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: children,
          );
          return Material(
            child: SizedBox(
              child: column,
              height: 160,
              width: px.screenWidth(),
            ),
          );
        });
  }
}
